package t3invoke;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import t3bean.MoneyRecordBean;
import t3bean.ParkingRecordBean;
import t3bean.ParkingSpaceBean;
import t3dao.MoneyRecordDao;
import t3dao.ParkingRecordDao;
import t3dao.ParkingSpaceDao;

public class GetCarAction implements Action {
	@Override
	public void getCar(Scanner sc) {
		System.out.println("输入车位号");
		Integer spaceid = Integer.parseInt(sc.nextLine());
		System.out.println("输入车牌号");
		String carid = sc.nextLine();
		ParkingRecordBean prb = new ParkingRecordBean();
		prb.setCarId(carid);
		prb.setSpaceId(spaceid);
		ParkingRecordDao prd = new ParkingRecordDao();
		List<ParkingRecordBean> list = new ArrayList<>();
		try {
			list = prd.select(prb);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size() == 0) {
			System.out.println("车牌号或车位号错误");
			return;
		}

		prb = list.get(0);

		ParkingSpaceBean psb = new ParkingSpaceBean();
		psb.setSpaceId(prb.getSpaceId());
		psb.setIsParking(0);
		psb.setCarid("null");
		ParkingSpaceDao psd = new ParkingSpaceDao();
		psd.update(psb);
		prd.delete(prb);
		LocalDateTime date2 = LocalDateTime.now();
		LocalDateTime date1 = prb.getParkingTime();
		int month = date2.getMonthValue() - date1.getMonthValue();
		int day = date2.getDayOfMonth() - date1.getDayOfMonth();
		int hour = date2.getHour() - date1.getHour();

		int money = 2 * (month * 30 * 24 + day * 24 + hour);
		MoneyRecordBean mrb = new MoneyRecordBean(prb.getCarId(), prb.getSpaceId(), money);
		mrb.setParkingtime(date1);
		mrb.setGettime(date2);
		MoneyRecordDao mrd = new MoneyRecordDao();
		mrd.insert(mrb);
		System.out.println("缴费成功");
	}

	@Override
	public void parkCar(Scanner sc) {
	}

	@Override
	public void showParkingSpace() {
	}

	@Override
	public void showMoneyRecord() {
	}

}
